<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
    /*
        分类添加页
    */
    public function getAdd()
    {
        // 查询所有分类
        // $cates = DB::table('dota_cate')->get();
        $cates = self::CateIndex();
        // dd($cates);
        return view('admin.cate.add',['cates'=>$cates]);
    }

    /*
        执行添加
    */
    public function postInsert(Request $request)
    {
        // dd($request->all());
        // 进行表单验证用户填写是否正确
          $this->validate($request, [
            'name' => 'required',
            'pid' => 'required',
          ],[
            'name.required' => '请填写分类名!!',
            'pid.required' => '请选择分类!!',
          ]);
        $data = $request->only(['name','pid']);
        // 判断pid是否为0
        if($data['pid'] == 0){
            $data['path'] = '0';
        }else{
            // 根据pid查询path
            $res = DB::table('dota_cate')->where('id',$data['pid'])->first();
            $data['path'] = $res->path.','.$data['pid'];
        }
        // dd($data);
        $data['status'] = 1; 
        // 执行数据插入操作
        $res = DB::table('dota_cate')->insert($data);
        if($res){
            return redirect('/admin/cate/index')->with('success','分类添加成功!!');
        }else{
            return back()->with('error','分类添加失败!!');
        }
    }

    /*
        分类展示页
    */
    public function getIndex(Request $request)
    {
        // $cates = DB::table('dota_cate')->get();
        // dd($cates);
        // $cates = self::CateIndex();
        // 获取分页大小
        $num = $request->input('num',10);
        if($request->input('keywords')){
            $cates = DB::table('dota_cate')->where('name','like','%'.$request->input('keywords').'%')->orderby('path')->orderby('id')->paginate($num); 
        }else{
            $cates = DB::table('dota_cate')->orderby('path')->orderby('id')->paginate($num);
        }


        foreach($cates as $k=>$v){
            // 把字符串变成数组
            $arr = explode(',',$v->path);
            // 获取长度
            $len = count($arr)-1;
            $cates[$k]->name = str_repeat('|----',$len).$v->name;
        }

        // 查询父级分类并显示
        foreach($cates as $k => $v){
            // 判断
            if($v->pid != 0){
                $res = DB::table('dota_cate')->where('id',$v->pid)->first();
                $cates[$k]->path = $res->name;
            }else{
                $cates[$k]->path = '顶级分类';
            }
        }
        // 提取当前请求的所有参数
        $data = $request->all();
        // 解析模板
        return view('admin.cate.index',['cates'=>$cates,'request'=>$data]);
    }

    /*
        按照格式查询分类列表
    */
    static public function CateIndex()
    {
        // 查询已有的分类
        $cates = DB::select('select *,concat(path,",",id) as paths from dota_cate order by paths');
        
        foreach($cates as $k=>$v){
            // 把字符串变成数组
            $arr = explode(',',$v->path);
            // 获取长度
            $len = count($arr)-1;
            $cates[$k]->name = str_repeat('|----',$len).$v->name;
        }
        return $cates;
    }

    /*
        分类修改操作
    */
    public function getEdit($id)
    {
        $cate = DB::table('dota_cate')->where('id',$id)->first();
        $cates = self::CateIndex();

        // dd($cate);

        // 解析模板
        return view('admin.cate.edit',['cate'=>$cate,'cates'=>$cates]);

    }

    /*
        执行分类修改
    */
    public function postUpdate(Request $request)
    {
        // dd($request->all());
        // 进行表单验证用户填写是否正确
          $this->validate($request, [
            'name' => 'required',
            'pid' => 'required',
          ],[
            'name.required' => '请填写分类名!!',
            'pid.required' => '请选择分类!!',
          ]);

        $id = $request->input('id');
        // dd($request->input('xpid'));
        // 判断是否修改了分类
        if($request->input('pid') != $request->input('xpid')){
            // 要修改分类,查询是否有子类
            $res = DB::table('dota_cate')->where('pid',$id)->count();

            if($res > 0){
                // 有子分类 不能修改
                return back()->with('error','有子分类,不能修改当前所属分类!!');
            }
        }

        // 可以修改
        $data = $request->only(['name','pid','status']);
        // dd($data);
        // 判断pid是否为0
        if($data['pid'] == 0){
            $data['path'] = '0';
        }else{
            // 根据pid查询path
            $res = DB::table('dota_cate')->where('id',$data['pid'])->first();
            $data['path'] = $res->path.','.$data['pid'];
        }

        // 执行数据修改操作
        $res = DB::table('dota_cate')->where('id',$id)->update($data);
        // 判断
        if($res){
            return redirect('/admin/cate/index')->with('success','分类修改成功!!');
        }else{
            return back()->with('error','分类修改失败!!');
        }
    }

    /*
        分类删除操作
    */
    public function postDelete(Request $request)
    {
        $id = $request->input('id');
        // 检测是否有子分类
        $res = DB::table('dota_cate')->where('pid',$id)->count();
        if($res > 0){
            // 给ajax返回
            return 1;
        }
        
        // 执行删除
        $res = DB::table('dota_cate')->where('id',$id)->delete();

        // 判断
        if($res){
            echo 2;
        }else{
            echo 3;
        }
    }
}
